﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class ResourceManager : MonoBehaviour
{
    private IList<string> searchPathList = new List<string>();
    private AssetBundleManifest mainfest;
    private IDictionary<string, AssetBundle> assetMap = new Dictionary<string, AssetBundle>();

    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {


    }


    public void AddSearchPath(string path)
    {
        if (!searchPathList.Contains(path))
        {
            searchPathList.Add(path);
        }
    }

    public string GetFilePath(string path)
    {
        string filepath = null;
#if UNITY_EDITOR
        filepath = Application.dataPath + "/StreamingAssets" + "/" + path;
#elif UNITY_IPHONE
        filepath = Application.dataPath +"/Raw"+ "/"+ path;
#elif UNITY_ANDROID
        filepath = "jar:file://" + Application.dataPath + "!/assets/"+ "/"+ path;
#endif
        return filepath;
    }

    public string IsFileExists(string path)
    {
        foreach (string p in searchPathList)
        {
            if (File.Exists(GetFilePath(p)))
                return p;
        }
        string filepath = GetFilePath(path);
        if (File.Exists(filepath))
        {
            return filepath;
        }
        return null;
    }
    public void LoadBundleConfigFile(string path)
    {
        string filepath = IsFileExists(path);
        if (filepath != null)
        {
            WWW mwww = WWW.LoadFromCacheOrDownload("file://" + filepath, (int)Random.Range(0f, int.MaxValue));
            while (mwww.isDone) { }

            if (!string.IsNullOrEmpty(mwww.error))
            {
                Debug.Log(mwww.error);
            }
            else {
                AssetBundle mab = mwww.assetBundle;
                mainfest = (AssetBundleManifest)mab.LoadAsset("AssetBundleManifest");
                mab.Unload(false);
            }

        }
    }
    private void LoadAssetFromBundleFile(string name)
    {
        string[] dps = mainfest.GetAllDependencies(name + Config.AB_PREFIX);
        for (int i = 0; i < dps.Length; i++)
        {
            string fname = dps[i].Substring(0, dps[i].Length - Config.AB_PREFIX.Length);
            if (!assetMap.ContainsKey(fname))
                LoadAssetFromBundleFile(fname);
        }
        string dUrl = IsFileExists(name + Config.AB_PREFIX);
        if (dUrl != null)
        {
            WWW dwww = WWW.LoadFromCacheOrDownload("file://" + dUrl, (int)Random.Range(0f, int.MaxValue));
            while (dwww.isDone) { }
            assetMap[name] = dwww.assetBundle;
        }
        Debug.Log(name + "加载完成");
    }
    public Object LoadGameObjectFromBundle(string name)
    {
        LoadAssetFromBundleFile(name.ToLower());
        if (assetMap.ContainsKey(name.ToLower()))
        {
            Object gobj = assetMap[name.ToLower()].LoadAsset(name);
            return gobj;
        }
        foreach (KeyValuePair<string, AssetBundle> entry in assetMap)
        {
            entry.Value.Unload(false);
        }
        return null;
    }
}
